<?php
//Data loading
//By Bayu

$pdf=new COAPDFReport("P","mm","Letter");
$pdf->DBConnection($DBConnection->dbc);
$pdf->ReadHeader(GetParam("nobkt",""));
$pdf->AddPage();
$pdf->GenerateReport();
$pdf->Output();

class COAPDFReport extends PDF {
  /**function COAPDFReport($Page,$Size,$PageMode) {
   parent::PDF($Page,$Size,$PageMode);
    $this->ColumnSize = array(85,30,30,30,30);
    $this->TableName = "tblcoa";
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);
  }**/

  function Header() {
	  $this->ColumnSize = array(85,30,30,30,30);
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);	
  
      $HeaderSize = array(70,50,25,50);
      $perush = GetCompanyName();
	  
	  //set left margin
      $this->SetLeftMargin(15); 
	  
      $this->Cell($HeaderSize[0],4,$perush,0,0,'L',0);
      $this->SetFont('arialn','',18);

      if (ereg("TK",$this->HeaderResult->nobkt)) $TransMode = "KAS";
      else $TransMode = "BANK";

      $this->Cell($HeaderSize[1],4,"TERIMA $TransMode",0,0,'L',0);
      $this->SetFont('arialn','',10);
      $this->Ln();
      $this->Cell($HeaderSize[0],4,GetOfficeLocation(),0,0,'L',0);
      $this->Cell($HeaderSize[1],4,"",0,0,'L',0);             
      $this->Cell($HeaderSize[2],4,"NO BUKTI",0,0,'L',0);
      $this->Cell($HeaderSize[3],4,": ".$this->TransNo,0,0,'L',0);
      $this->Ln();
      $this->Cell($HeaderSize[0],4,"",0,0,'L',0);
      $this->Cell($HeaderSize[1],4,"",0,0,'L',0);             
      $this->Cell($HeaderSize[2],4,"TGL. MUTASI",0,0,'L',0);
      $this->Cell($HeaderSize[3],4,": ".$this->HeaderResult->tglbkt,0,0,'L',0);
      $this->Ln();
      $this->Cell(30,4,"DITERIMA DARI","LT",0,'L',0);
      $this->Cell(5,4,": ".$this->HeaderResult->debnam,'T',0,'L',0);             
      $this->Cell($HeaderSize[2],4,"",'T',0,'L',0);
      $this->Cell($HeaderSize[3],4,"",'TR',0,'L',0);
      $this->Cell(10,4,"",0,0,'L',0);
      $this->Cell(25,4,"VALUTA",0,0,'L',0);
      $this->Cell($HeaderSize[3],4,": ".$this->HeaderResult->curr,0,0,'L',0);
      $this->Ln();
      $this->Cell(30,4,"UNTUK",'LB',0,'L',0);
      $this->Cell(5,4,": ".$this->HeaderResult->uraian,'B',0,'L',0);
      $this->Cell($HeaderSize[2],4,"",'B',0,'L',0);
      $this->Cell($HeaderSize[3],4,"",'BR',0,'L',0);
      $this->Cell(10,4,"",0,0,'L',0);
      $this->Cell(25,4,"KURS",0,0,'L',0);
      $this->Cell($HeaderSize[3],4,": ".FormatNumber($this->HeaderResult->rate),0,0,'L',0);
      $this->Ln();
      $this->Ln();
      $this->Cell(10,4,"NO.",1,0,'C',0);
      $this->Cell(60,4,"URAIAN",1,0,'C',0);
      $this->Cell(25,4,"JUMLAH",1,0,'C',0);
      $this->Cell(20,4,"SKADA",1,0,'C',0);
      $this->Cell(20,4,"UU",1,0,'C',0);
      $this->Cell(20,4,"ORDER",1,0,'C',0);
      $this->Cell(25,4,"REFERENSI",1,0,'C',0);
      $this->Ln();
  }

  function DBConnection ($Conn) {
    $this->DBConnection = $Conn; 
  }

  function ReadHeader($TransNo) {
    //Header
    $this->TransNo=$TransNo;
    $SQL = "SELECT *,DATE_FORMAT(tglbkt,'%d/%m/%Y') as tglbkt FROM ais_memorial WHERE nobkt='".$TransNo."' AND urut=999 ";
    $this->HeaderResult = $this->DBConnection->get_row($SQL);
  }

  function GenerateReport() {
    //Detail
//print $this->HeaderResult->curr;
    $DecimalPoint = ($this->HeaderResult->curr=="IDR")?0:2;
    $SQL = "SELECT * FROM ais_memorial WHERE nobkt='".$this->TransNo."' ORDER BY urut";
    
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
    
    $SQL_2 = "SELECT * FROM ais_memorial 
	WHERE nobkt='".$MyResults[0]['noref']."' ORDER BY urut";
    
    $MyResults_2 = $this->DBConnection->get_results($SQL_2,ARRAY_A);
    
       
    foreach ($MyResults as $rows) {
      if ($rows['urut']<>999) {
        $this->Cell(10,4,$rows['urut'].".",'LR',0,'R',0);
        $this->Cell(60,4,$rows['uraian'],'LR',0,'L',0);
        $this->Cell(25,4,FormatNumber($rows['jumlah'],$DecimalPoint),'LR',0,'R',0);
        $this->Cell(20,4,$rows['skada'],'LR',0,'C',0);
        $this->Cell(20,4,$rows['kdact'],'LR',0,'C',0);
        $this->Cell(20,4,$rows['noorder'],'LR',0,'C',0);
        $this->Cell(25,4,$rows['noref'],'LR',0,'C',0);
        $this->Ln();
        
        $this->Cell(10,4,"",'LR',0,'R',0);
        $this->Cell(60,4,"Job Order :".$MyResults_2[0]['jo_code'],'LR',0,'L',0);
        $this->Cell(25,4,"",'LR',0,'R',0);
        $this->Cell(20,4,"",'LR',0,'C',0);
        $this->Cell(20,4,"",'LR',0,'C',0);
        $this->Cell(20,4,"",'LR',0,'C',0);
        $this->Cell(25,4,"",'LR',0,'C',0);
        $this->Ln();
        
      }
    }
    $this->BlankLine();$this->BlankLine();
    $this->Cell(10,4,$rows['urut'].".",1,0,'R',0);
    $this->Cell(60,4,$rows['uraian'],1,0,'L',0);
    $this->Cell(25,4,FormatNumber($rows['jumlah'],$DecimalPoint),1,0,'R',0);
    $this->Cell(20,4,$rows['skada'],1,0,'C',0);
    $this->Cell(20,4,$rows['kdact'],1,0,'C',0);
    $this->Cell(20,4,$rows['noorder'],1,0,'C',0);
    $this->Cell(25,4,$rows['noref'],1,0,'C',0);
    $this->Ln();
    $terbilang = new intToRupiah();
    $terbilang->setTail($rows['curr']);        
    $terbilang->setNumber($rows['jumlah']);

    $this->Cell(120,4,"TERBILANG : ".strtoupper($terbilang->getCurrency()),0,0,'L',0);
    $this->Ln();
    $this->Ln();
    
    //Footer
    $this->footer_edit($rows['jumlah'],$rows['curr']);
                
  }

  function BlankLine() {
        $this->Cell(10,4,"",'LR',0,'R',0);
        $this->Cell(60,4,"",'LR',0,'L',0);
        $this->Cell(25,4,"",'LR',0,'R',0);
        $this->Cell(20,4,"",'LR',0,'C',0);
        $this->Cell(20,4,"",'LR',0,'C',0);
        $this->Cell(20,4,"",'LR',0,'C',0);
        $this->Cell(25,4,"",'LR',0,'C',0);
        $this->Ln();
  }

  function footer_edit($total,$curr) {
	  
	    global $DBConnection,$DBGeneral;
	    
	    
	    //Currency Rate
		$SQLGetKurs = "SELECT * FROM ais_kurssi WHERE curr = 'USD' ORDER BY tglbkt DESC";
		$ResultGetKurs  = $DBConnection->dbc->get_row($SQLGetKurs,ARRAY_A);   

		$kursUSD = $ResultGetKurs['rate'];
		
		
		if($Curr == "USD")
		{
			$totalAll_menyetujui = $total * $kursUSD;
			$totalAll_tgjwb = $total * $kursUSD;
		}
		
		else
		{
			$totalAll_menyetujui = $total;
			$totalAll_tgjwb = $total;
		}
		
		
		
		$ttd_menyetujui = "";
		$ttd_tgjwb = "";
		
		
		$SQLGetSetujuBayar = "SELECT * FROM tblreportsign as a 
							  LEFT JOIN ".$DBGeneral.".tblstaff as b on a.staff_id = b.staff_id   
		WHERE a.assignment = 'setuju_bayar' AND a.report_kind = 'bs_jo' ORDER BY a.report_sign_id ASC ";
		
		$GetPersonSetujuBayar = $DBConnection->dbc->get_results($SQLGetSetujuBayar, ARRAY_A);
		
		if(!empty($GetPersonSetujuBayar))
		{
		
			foreach($GetPersonSetujuBayar as $idx => $val)
			{
			   $range_left = $val['range_left'];
			   $range_right = $val['range_right'];
			   
			   if(($totalAll_menyetujui >= $range_left) && ($totalAll_menyetujui <= $range_right))
			   {
					$ttd_menyetujui = $val["nama"];
			   }	
			
			}
	   
		}				
		
		
		$SQLGetTanggungJawab = "SELECT * FROM tblreportsign as a 
							  LEFT JOIN ".$DBGeneral.".tblstaff as b on a.staff_id = b.staff_id  
		WHERE a.assignment = 'penanggung_jawab' AND a.report_kind = 'bs_jo' ORDER BY a.report_sign_id ASC ";
		
		$GetPersonTanggungJawab = $DBConnection->dbc->get_results($SQLGetTanggungJawab, ARRAY_A);
		
		
		if(!empty($GetPersonTanggungJawab))
		{
		
			foreach($GetPersonTanggungJawab as $idx => $val)
			{
			   $range_left = $val['range_left'];
			   $range_right = $val['range_right'];
			   
			   if(($totalAll_tgjwb >= $range_left) && ($totalAll_tgjwb <= $range_right))
			   {
					$ttd_tgjwb = $val["nama"];
			   }	
			
			}
		
		}
		
	    
	    $SQL = "SELECT * FROM ais_memorial WHERE nobkt='".$this->TransNo."' ORDER BY urut";
        $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
		
			   
        $this->Cell(60,4,"SETUJU TERIMA",1,0,'C',0);
        $this->Cell(60,4,"PENYETOR",1,0,'C',0);
        $this->Cell(60,4,"PENERIMA",1,0,'C',0);
        $this->Ln();
        $this->Cell(15,4,"NAMA",1,0,'C',0);
        $this->Cell(45,4,$ttd_menyetujui,1,0,'C',0);
        $this->Cell(15,4,"NAMA",1,0,'C',0);
        $this->Cell(45,4,$ttd_tgjwb,1,0,'C',0);
        $this->Cell(15,4,"NAMA",1,0,'C',0);
        $this->Cell(45,4,$MyResults[0]['debnam'],1,0,'C',0);
        $this->Ln();
        
        /*
        $GetPerson = $this->DBConnection->get_row("SELECT * FROM tblprofile", ARRAY_A);
        $this->Cell(15,4,"NAMA",1,0,'C',0);
        $this->Cell(45,4,$GetPerson["ttdtktb1"],1,0,'C',0);
        $this->Cell(15,4,"NAMA",1,0,'C',0);
        $this->Cell(45,4,$MyResults[0]['debnam'],1,0,'C',0);
        $this->Cell(15,4,"NAMA",1,0,'C',0);
        $this->Cell(45,4,$GetPerson["ttdtktb2"],1,0,'C',0);
        $this->Ln();
        */
        
        $this->Cell(15,4,"","LR",0,'C',0);
        $this->Cell(45,4,"","LR",0,'C',0);
        $this->Cell(60,4,"","LR",0,'C',0);
        $this->Cell(60,4,"","LR",0,'C',0);
        $this->Ln();        
        $this->Cell(15,4,"TANDA","LR",0,'C',0);
        $this->Cell(45,4,"","LR",0,'C',0);
        $this->Cell(60,4,"","LR",0,'C',0);
        $this->Cell(60,4,"","LR",0,'C',0);
        $this->Ln();
        $this->Cell(15,4,"TANGAN","LR",0,'C',0);
        $this->Cell(45,4,"","LR",0,'C',0);
        $this->Cell(60,4,"","LR",0,'C',0);
        $this->Cell(60,4,"","LR",0,'C',0);
        $this->Ln();
        $this->Cell(15,4,"","LRB",0,'C',0);
        $this->Cell(45,4,"","LRB",0,'C',0);
        $this->Cell(60,4,"","LRB",0,'C',0);
        $this->Cell(60,4,"","LRB",0,'C',0);
        $this->Ln();        
        $this->Cell(180,4,"",1,0,'C',0);
        $this->Ln();       
  }

}
?>
